if ("undefined" == typeof(SessionHpps))
    var SessionHpps = {
    	prefs : function() {
    		Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
		},

		init : function () {
			var appcontent = document.getElementById("appcontent");
			if( appcontent )
            	appcontent.addEventListener("DOMContentLoaded", SessionHpps.save, true);
            var oldVersion = true;
		    if( typeof( JSON ) === "object" || typeof( JSON ) === "function" ){
		        if( typeof( JSON.stringify ) === "function" && typeof( JSON.parse ) === "function" ){
		            oldVersion = false;
		        }
		    }
		    if(oldVersion){
		        var Ci = Components.interfaces;
		        var Cc = Components.classes;
		        var nativeJSON = Cc["@mozilla.org/dom/json;1"].createInstance(Ci.nsIJSON);
		        SessionHpps.fromJSON = nativeJSON.decode;
		        SessionHpps._toJSON = nativeJSON.encode;
		    }else{
		        SessionHpps.fromJSON = JSON.parse;
		        SessionHpps._toJSON = JSON.stringify;
		    }
		},
		
		save : function () {
			
			var html = content.document.documentElement.outerHTML;
			var current_url = content.document.location.href;
			var clean_url = current_url.replace( /#.*?$/gi , "" );
			var root_url = current_url.replace( /^(https?:\/\/[^\/]+\/?).*?$/gi, "$1" );
			var relative_url = current_url.replace( /^(.*?\/)[^\/]+$|^(.*?\/)$/gi, "$1" );
			
			var resolveLink = function( url ){
				if( url.match( /^\s*https?:\/\//gi ) )
					return url;
				else if( url[0] == "/" )
					return root_url + url.substr(1);
				else if( url[0] == "#" )
					return clean_url + url;
				else
					return relative_url + url;
			}
			
			html = html.replace( /\s(href|src)=([\'\"])(.*?)\2/gi , function( match, s1, s2, s3, offset, s0 ){
				return " "+s1+"="+s2+resolveLink( s3 )+s2;
			});
			
			Application.console.log( html );
			
			Application.console.log( SessionHpps.getCookies() );
			
		},
		
		restore : function () {
		
		},
		
		getCookies : function() {
			var cookieMgr = Components.classes["@mozilla.org/cookiemanager;1"]  
                  .getService(Components.interfaces.nsICookieManager);  
			
			var dumped = new Array();
			
			for (var e = cookieMgr.enumerator; e.hasMoreElements();) {  
			    var cookie = e.getNext().QueryInterface(Components.interfaces.nsICookie);   
		  	    dumped.push( cookie );
			}
			
			return SessionHpps.toJSON( dumped );
		},
		
		toJSON: function(obj) {
		    return SessionHpps.safeJSON(
		        SessionHpps._toJSON( obj )
		    );
		},
		
		safeJSON: function (json) {
		    if( typeof(json) !== "string" )
		        json = SessionHpps._toJSON( json );
		    return json.replace(
		        /[^\w\d\s\.,:_\-\{\}\"\'\[\]\+\\\/\?\$\(\)\=]/g,
		        function (chr){
		            //to hex
		            chr = chr.charCodeAt(0).toString(16);
		            //pad with zero if needed
		            return "\\u" + ( '000' + chr ).slice(-4);
		        }
		    );
		}
	};
window.addEventListener("load", SessionHpps.init, false);
